//
//  FullScreenViewController.m
//  NuwaRobot
//
//  Created by Kendama on 2017/12/6.
//  Copyright © 2017年 Kendama. All rights reserved.
//

#import "FullScreenViewController.h"
#import "FullScreenView.h"
@interface FullScreenViewController ()<FullScreenViewDelegate>

/** 背景按钮 **/
@property(nonatomic,strong) UIButton *bgButton;
// 定时器
@property (nonatomic, strong)dispatch_source_t timer;

@end

@implementation FullScreenViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = rgba(0,0,0,0.8);
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}

#pragma mark -- 定时刷新
- (void)startTiming {
    
    NSTimeInterval period = 1.0; //设置时间间隔
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    self.timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    dispatch_source_set_timer(self.timer, dispatch_walltime(NULL, 0), period * NSEC_PER_SEC, 0); //每秒执行
    dispatch_source_set_event_handler(self.timer, ^{
        //在这里执行事件
        [self setNowDateString];
    });
    dispatch_resume(self.timer);
}

- (void)stopTiming
{
    if (self.timer)
    {
        dispatch_cancel(self.timer);
    }
}

#pragma mark --- 点击事件
- (void)didClickBgButton
{
    if (self.closeFullBlock)
    {
        self.closeFullBlock();
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.05 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.view removeFromSuperview];
            [self removeFromParentViewController];
            [self stopTiming];
        });
    }


}

- (void)didClickLeftRotationButton
{
    
}

- (void)didClickRightRotationButton
{
    
}

- (void)didClickShowControlButton
{
    self.fullScreenView.showButton.selected = !self.fullScreenView.showButton.selected;
    self.fullScreenView.aimingCircleView.hidden = !self.fullScreenView.showButton.selected;
}

- (void)didClickPlayButton
{
    
}


#pragma mark --- 事件处理
- (void)setNowDateString
{
    NSDate *date = [NSDate date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    dispatch_async(dispatch_get_main_queue(), ^{
        self.fullScreenView.dateLabel.text = [formatter stringFromDate:date];
    });
}

#pragma mark --- 代理
- (void)fullScreenViewDidClickButton:(NSInteger)index
{
    NSLog(@"click---%li",(long)index);
    [[DataManager sharedInstance] sendCode:@"1052" data:@{@"action":@(index).stringValue}];

}

#pragma mark --- 懒加载
- (UIButton *)bgButton
{
    if(!_bgButton)
    {
        UIView *superView = self.view;
        _bgButton = [[UIButton alloc]init];
        [superView addSubview:_bgButton];
        [_bgButton addTarget:self action:@selector(didClickBgButton) forControlEvents:UIControlEventTouchDown];

    }
    return _bgButton;
}

- (FullScreenView *)fullScreenView
{
    if(!_fullScreenView)
    {
        UIView *superView = self.view;
        _fullScreenView = [[FullScreenView alloc]init];
        [superView addSubview:_fullScreenView];
        _fullScreenView.delegate = self;
        [_fullScreenView.showButton addTarget:self action:@selector(didClickShowControlButton) forControlEvents:UIControlEventTouchUpInside];
    }
    return _fullScreenView;
}


- (void)masLayoutSubview
{
    __weak UIView *superView = self.view;
    [self.bgButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(superView);
    }];
    
    [self.fullScreenView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(superView.mas_top).with.offset(px1920Height(212));
        make.centerX.mas_equalTo(superView.mas_centerX);
        make.height.width.mas_equalTo(px1920Height(1000));
    }];

    self.fullScreenView.backgroundColor = [UIColor blueColor];
}


- (void)setNavigationStyle
{
    [self setLeftButtonImage:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
